/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import xowl.gmi.environment.Locale;
import xowl.gmi.environment.Options;
import xowl.gmi.view.diagram.Widget;
import xowl.gmi.view.diagram.Diagram;

/**
 *
 * @author Laurent WOUTERS
 */
public class DiagramView extends javax.swing.JPanel {
    public static interface Actions {
        void onRemoved();
    }
    
    private Actions actions;
    private ViewStyle style;
    private Toolboxes toolboxes;
    private javax.swing.JScrollPane scroll;
    private Diagram diagram;
    
    public ViewStyle getStyle() { return style; }
    public Toolboxes getToolboxes() { return toolboxes; }
    
    public DiagramView(Actions actions, Options options, Locale locale) {
        this.actions = actions;
        style = new ViewStyle();
        toolboxes = new Toolboxes();
        diagram = new Diagram(options, locale);
        scroll = new javax.swing.JScrollPane(diagram);
        scroll.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override public void componentResized(java.awt.event.ComponentEvent evt) {
                diagram.onUpdated(scroll.getViewport().getSize());
            }
        });
        this.setOpaque(false);
        this.setLayout(new java.awt.BorderLayout());
        this.add(scroll, java.awt.BorderLayout.CENTER);
    }
    
    public void addToolboxPart(CollapsePanel panel) { toolboxes.addPart(panel); }
    
    public void setDiagramRoot(Widget widget) {
        diagram.setRoot(widget);
        onDiagramUpdated();
    }
    
    public void onDiagramUpdated() {
        diagram.onUpdated(scroll.getViewport().getSize());
    }
    public void onDiagramRemoved() {
        actions.onRemoved();
    }
    
    public void beginConnecting() { diagram.beginConnecting(); }
    public void endConnecting() { diagram.endConnecting(); }
    
    public void zoomPlus() { diagram.zoomIncrease();}
    public void zoomMinus() { diagram.zoomDecrease(); }
    public void zoomReset() { diagram.zoomReset(); }
    public void zoomFit() { diagram.zoomToFit(scroll.getSize()); }
    
    public java.awt.image.BufferedImage exportBMP() { return diagram.exportBMP(); }
    public org.w3c.dom.Document exportSVG() { return diagram.exportSVG(); }
}
